package com.example.recuitactivity.myMainActivity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.CollectionActivity;
import com.example.recuitactivity.R;
import com.example.recuitactivity.candidate_fragment.CandidateDeliveredFragment;
import com.example.recuitactivity.candidate_fragment.CandidateInfoFragment;
import com.example.recuitactivity.candidate_fragment.CandidateInterviewFragment;
import com.example.recuitactivity.candidate_fragment.CandidateResumeFragment;
import com.example.recuitactivity.helper.DbOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class CandidateMainActivity extends FragmentActivity {
    //声明ViewPager
    private ViewPager mViewPager;
    //适配器
    private FragmentPagerAdapter mAdapter;
    //装载Fragment的集合
    private List<Fragment> mFragments;
    //三个Tab对应的布局
    private LinearLayout mTab1,mTab2,mTab3,mTab4;
    private TextView mText1,mText2,mText3,mText4;
    private View view1,view2,view3,view4;
    private TextView tv_user;
    private ImageView iv_menu,iv_lingdang;

    private String username;
    private DbOpenHelper dbOpenHelper;
    private RotateAnimation rotateAnimation;

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case  R.id.id_recuit_info:
                    selectTab(0);
                    break;
                case R.id.id_delivered:
                    selectTab(1);
                    break;
                case R.id.id_interview:
                    selectTab(2);
                    break;
                case R.id.id_myresume:
                    selectTab(3);
                    break;
                case R.id.iv_candidate_topbar_lingdang:
                    Intent intent=new Intent(CandidateMainActivity.this, CollectionActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("username",username);
                    long cid=dbOpenHelper.getCandidateIdByUsername(username);
                    bundle.putLong("candidates_id",cid);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
            }
        }
    };

    public void initUI() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager_candidate);

        mTab1 = (LinearLayout) findViewById(R.id.id_recuit_info);
        mTab2 = (LinearLayout) findViewById(R.id.id_delivered);
        mTab3 = (LinearLayout) findViewById(R.id.id_interview);
        mTab4 = (LinearLayout) findViewById(R.id.id_myresume);

        mText1=(TextView) findViewById(R.id.tv3);
        mText2=(TextView) findViewById(R.id.tv4);
        mText3=(TextView) findViewById(R.id.tv5);
        mText4=(TextView) findViewById(R.id.tv6);

        view1=(View) findViewById(R.id.view1);
        view2=(View) findViewById(R.id.view2);
        view3=(View) findViewById(R.id.view3);
        view4=(View) findViewById(R.id.view4);

        tv_user=(TextView) findViewById(R.id.tv_logined_user1);
        iv_menu=(ImageView) findViewById(R.id.iv_menu_candidate);
        iv_lingdang=(ImageView) findViewById(R.id.iv_candidate_topbar_lingdang);
        registerForContextMenu(iv_menu);
    }

    private void initEvents(){
        //设置两个Tab的点击事件
        mTab1.setOnClickListener(listener);
        mTab2.setOnClickListener(listener);
        mTab3.setOnClickListener(listener);
        mTab4.setOnClickListener(listener);
        iv_lingdang.setOnClickListener(listener);
    }

    private void initDatas(){
        dbOpenHelper=new DbOpenHelper(CandidateMainActivity.this);
        username=getIntent().getExtras().getString("username");
        judgeCollectionUpdated(username);//判断是否需要铃铛提示
        tv_user.setText("欢迎  "+username);
        mFragments=new ArrayList<>();
        //将两个Fragment加入集合中
        mFragments.add(new CandidateInfoFragment(username));
        mFragments.add(new CandidateDeliveredFragment(username));
        mFragments.add(new CandidateInterviewFragment(username));
        mFragments.add(new CandidateResumeFragment(username));

        //初始化适配器
        mAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };

        //设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //将ViewPager的预加载个数设置为你的所有Fragment的数量,解决滑动时候轮播图出现空白的问题
        mViewPager.setOffscreenPageLimit(mFragments.size());
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滚动事件
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //页面选中事件
            @Override
            public void onPageSelected(int position) {
                //设置position对应的集合中的fragment
                changeFragment(position);
            }
            //页面滚动状态改变事件
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void changeFragment(int position){
        mViewPager.setCurrentItem(position);
        resetStyle();
        selectTab(position);
    }

    private void resetStyle() {
        mText1.setTextSize(20);
        mText2.setTextSize(20);
        mText3.setTextSize(20);
        mText4.setTextSize(20);

        view1.setVisibility(View.INVISIBLE);
        view2.setVisibility(View.INVISIBLE);
        view3.setVisibility(View.INVISIBLE);
        view4.setVisibility(View.INVISIBLE);
    }

    //根据点击的Tab设置对应的ImageButton为选中的颜色
    private void selectTab(int i) {
        switch (i) {
            case 0:
                mText1.setTextSize(22);
                view1.setVisibility(View.VISIBLE);
                break;
            case 1:
                mText2.setTextSize(22);
                view2.setVisibility(View.VISIBLE);
                break;
            case 2:
                mText3.setTextSize(22);
                view3.setVisibility(View.VISIBLE);
                break;
            case 3:
                mText4.setTextSize(22);
                view4.setVisibility(View.VISIBLE);
                break;
        }
        //设置当前点击的Tab所对应的页面
        mViewPager.setCurrentItem(i);
    }

    public void judgeCollectionUpdated(String username){
        long cid=dbOpenHelper.getCandidateIdByUsername(username);
        rotateAnimation=null;

        if(dbOpenHelper.getCandidateFlagById(cid)==1){//该用户的收藏岗位中存在更新

            iv_lingdang.setVisibility(View.VISIBLE);
            //以图片的顶边中点为中心，进行左右摆动
            rotateAnimation = new RotateAnimation(0, 15, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f);
            rotateAnimation.setInterpolator(new CycleInterpolator(2));//周期插值器
            rotateAnimation.setRepeatCount(-1);//重复次数，不停止
            rotateAnimation.setDuration(1120);
            iv_lingdang.setAnimation(rotateAnimation);
            rotateAnimation.start();
        }else{
            iv_lingdang.setVisibility(View.INVISIBLE);
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_candidate_main);
        initUI();
        selectTab(0);//初始页面为主页
        initEvents();//初始化事件
        initDatas();//初始化数据
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.main_menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.menu_logout:
                finish();
                Toast.makeText(this,"注销成功！",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        judgeCollectionUpdated(username);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(rotateAnimation!=null){
            rotateAnimation.cancel();
        }
    }
}